package com.ml.mall.conversion;

import com.ml.mall.bean.req.BaseDateRangeReq;
import com.ml.mall.bean.req.admin.purchase.PurchaseQueryOneReq;
import com.ml.mall.bean.res.admin.AdminOverviewRes;
import com.ml.mall.bean.res.admin.AdminPurchaseRes;
import com.ml.mall.bean.res.admin.PurchaseQueryOneRes;
import com.ml.mall.bean.res.admin.vo.*;
import com.ml.mall.bean.vo.CouponVO;
import com.ml.mall.bean.vo.DateValueVO;
import com.ml.mall.utils.DateTransformUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

/**
 * Created by kyyang on 2021/6/9 23:54
 */
public class AnalyticsAdminServiceHelp {
    
    
    public static AdminOverviewRes adminOverviewConversion(BaseDateRangeReq req, Future<BigDecimal> amtFuture, Future<List<AdminOverviewSalesByCountryVO>> adminOverviewSalesBySateVOFuture, Future<List<AdminOverviewSalesByCategoryVO>> adminOverviewSalesByCategoryVOFuture, Future<List<AdminOverviewSalesByIndustryVO>> adminOverviewSalesByIndustryVOFuture, Future<List<DateValueVO>> registerCompanyBuyerFuture,Future<List<DateValueVO>> registerCompanySupplierFuture, Future<List<CouponVO>> couponUseFuture) throws Exception{

        AdminOverviewRes res = new AdminOverviewRes();

        if(adminOverviewSalesBySateVOFuture!=null){
            List<AdminOverviewSalesByCountryVO> adminOverviewSalesByCountryVOS = adminOverviewSalesBySateVOFuture.get();
            res.setSalesByState(adminOverviewSalesByCountryVOS);
        }
        if(adminOverviewSalesByCategoryVOFuture!=null){
            List<AdminOverviewSalesByCategoryVO> adminOverviewSalesByCategoryVOS = adminOverviewSalesByCategoryVOFuture.get();
            res.setSalesByCategory(adminOverviewSalesByCategoryVOS);
        }
        if(adminOverviewSalesByIndustryVOFuture!=null){
            List<AdminOverviewSalesByIndustryVO> adminOverviewSalesByIndustryVOS = adminOverviewSalesByIndustryVOFuture.get();
            res.setSalesByIndustry(adminOverviewSalesByIndustryVOS);
        }
        if(registerCompanyBuyerFuture!=null){
            List<DateValueVO> dateValueVOS = registerCompanyBuyerFuture.get();
            Map<String, Double> collect = dateValueVOS.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
            List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), collect);
            res.setRegisterCompanyBuyer(curDateMonthDataInit);
        }
        if(registerCompanySupplierFuture!=null){
            List<DateValueVO> dateValueVOS = registerCompanySupplierFuture.get();
            Map<String, Double> collect = dateValueVOS.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
            List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), collect);
            res.setRegisterCompanySupplier(curDateMonthDataInit);
        }
        if(couponUseFuture!=null){
            List<CouponVO> dateValueVOS = couponUseFuture.get();
            res.setCouponUse(dateValueVOS);
        }
        BigDecimal amt = BigDecimal.ZERO;
        if(amtFuture!=null){
            amt = amtFuture.get();
        }
        res.setTotalSales(amt);

        return res;

    }


    public static AdminPurchaseRes adminPurchaseOrderArrConversion(BaseDateRangeReq req, Future<List<DateValueVO>> purchaseOrderArrFuture, Future<List<DateValueVO>> purchaseSpentArrFuture) throws ExecutionException, InterruptedException {
        AdminPurchaseRes res = new AdminPurchaseRes();
        if(purchaseOrderArrFuture!=null){
            List<PurchaseOrderArrVO> purchaseOrderArr = new ArrayList<>();
            List<DateValueVO> dateValueVOS = purchaseOrderArrFuture.get();
            if(dateValueVOS!=null&&dateValueVOS.size()>0){
                Map<String, List<DateValueVO>> collect = dateValueVOS.stream().collect(Collectors.groupingBy(DateValueVO::getName));
                for(Map.Entry<String, List<DateValueVO>> entry: collect.entrySet()){
                    PurchaseOrderArrVO purchaseOrderArrVO = new PurchaseOrderArrVO();
                    String key = entry.getKey();
                    List<DateValueVO> value = entry.getValue();
                    purchaseOrderArrVO.setPurchaserName(key);
                    Long v = value.stream().collect(Collectors.summingLong(e -> e.getValue().longValue()));
                    purchaseOrderArrVO.setTotalPo(v);
                    Map<String, Double> result = value.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
                    List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), result);
                    purchaseOrderArrVO.setDayDetail(curDateMonthDataInit);
                    purchaseOrderArr.add(purchaseOrderArrVO);
                }
            }
            res.setPurchaseOrderArr(purchaseOrderArr);
        }
        if(purchaseSpentArrFuture!=null){
            List<PurchaseSpentReportVO> purchaseSpentReportVOS = new ArrayList<>();
            List<DateValueVO> dateValueVOS = purchaseSpentArrFuture.get();
            if(dateValueVOS!=null&&dateValueVOS.size()>0){
                Map<String, List<DateValueVO>> collect = dateValueVOS.stream().collect(Collectors.groupingBy(DateValueVO::getName));
                for(Map.Entry<String, List<DateValueVO>> entry: collect.entrySet()){
                    PurchaseSpentReportVO purchaseSpentReportVO = new PurchaseSpentReportVO();
                    String key = entry.getKey();
                    List<DateValueVO> value = entry.getValue();
                    purchaseSpentReportVO.setCompanyName(key);
                    Double v = value.stream().collect(Collectors.summingDouble(e->e.getValue().doubleValue()));
                    purchaseSpentReportVO.setTotalSpent(BigDecimal.valueOf(v));
                    Map<String, Double> result = value.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
                    List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), result);
                    purchaseSpentReportVO.setDayDetail(curDateMonthDataInit);
                    purchaseSpentReportVOS.add(purchaseSpentReportVO);
                }
            }
            res.setPurchaseSpentReport(purchaseSpentReportVOS);
        }
        return res;
    }

    public static PurchaseQueryOneRes adminPurchaseQueryOneDetailConversion(PurchaseQueryOneReq req, Future<List<DateValueVO>> purchaseOrderArrFuture, Future<List<DateValueVO>> purchaseSpentArrFuture) throws Exception {
        PurchaseQueryOneRes res = new PurchaseQueryOneRes();
        Long pos = 0L;
        BigDecimal spent = BigDecimal.ZERO;
        if(purchaseOrderArrFuture!=null){
            List<DateValueVO> value = purchaseOrderArrFuture.get();
            Long v = value.stream().collect(Collectors.summingLong(e -> e.getValue().longValue()));
            pos = v;
            Map<String, Double> result = value.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
            List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), result);
            res.setTotalPoDetail(curDateMonthDataInit);
        }
        if(purchaseSpentArrFuture != null){
            List<DateValueVO> value = purchaseSpentArrFuture.get();
            Double v = value.stream().collect(Collectors.summingDouble(e -> e.getValue().doubleValue()));
            spent = BigDecimal.valueOf(v);
            Map<String, Double> result = value.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
            List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), result);
            res.setSpentDetail(curDateMonthDataInit);
        }
        res.setTotalPo(pos);
        res.setTotalSpent(spent);
        return res;

    }
}
